﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.SCHEDULE.Components;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.SCHEDULE.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += new EventHandler(AddEditItem_Update);
            this.Delete += new EventHandler(AddEditItem_Delete);            
            base.OnInit(e);
            
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Modules.SCHEDULE.Components.Item item = Modules.SCHEDULE.Components.Item.Load(this.ItemID);
                item.Delete();
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            Modules.SCHEDULE.Components.Item item = new Modules.SCHEDULE.Components.Item();
            if (this.ItemID > 0)
            {
                item = Modules.SCHEDULE.Components.Item.Load(this.ItemID);
            }

            //Category
            item.DepartmentID = Convert.ToInt32(cbCategory.SelectedValue);

            //Từ ngày
            item.FromDate = Convert.ToDateTime(txtTuNgay.Text);
            
            //Đến ngày
            item.ToDate = Convert.ToDateTime(txtDenNgay.Text);
            
            // Tiêu đề.
            item.Title = txtTitle.Text;

            // Nội dung.
            item.Description = HttpUtility.HtmlEncode(txtContent.Text);

            item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
            item.CreatedDate = DateTime.Now;                

            if (this.ItemID == 0)
            {
                this.ItemID = item.Insert();
            }
            else
            {
                item.ID = this.ItemID;
                item.Update();
            }
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.Item);
            Response.Redirect(url);            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbCategory.DataSource = ASPNETVN.PORTAL.Components.Share.Department.SelectAll(this.ModuleID);
                cbCategory.DataBind();
                if (this.ItemID > 0)
                {
                    Components.Item item = Components.Item.Load(this.ItemID);
                    if (item != null)
                    {    
                        //Category
                        cbCategory.SelectedValue = item.DepartmentID.ToString();

                        //Tu ngay
                        if (item.FromDate.ToString()!="")
                            txtTuNgay.Text = item.FromDate.ToString().Substring(0,item.FromDate.ToString().IndexOf(" "));

                        //Den ngay
                        if (item.ToDate.ToString() != "")
                            txtDenNgay.Text = item.ToDate.ToString().Substring(0, item.ToDate.ToString().IndexOf(" "));

                        // Tiêu đề.
                        txtTitle.Text = item.Title;
                        
                        // Tác giả.
                        txtNguoiTao.Text = item.CreatedBy;

                        //Ngay tao
                        if (item.CreatedDate.ToString() != "")
                            txtNgayTao.Text = item.CreatedDate.ToString().Substring(0, item.CreatedDate.ToString().IndexOf(" "));

                        //Nội dung.
                        txtContent.Text = HttpUtility.HtmlDecode(item.Description);                        
                    }
                }
                else
                {
                    txtNguoiTao.Text = PortalSecurity.CurrentUser.Identity.Email;
                    txtNgayTao.Text = DateTime.Now.ToString();
                    btnDelete.Visible = false;                    
                }
            }
        }
          
    }
}